package com.two.lbms.lzl.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.two.lbms.base.BaseService;
import com.two.lbms.dao.DictionaryListMapper;
import com.two.lbms.lzl.query.DictionaryListQuery;
import com.two.lbms.utils.AssertUtil;
import com.two.lbms.vo.DictionaryList;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DictionaryListService extends BaseService<DictionaryList,Integer> {
    @Resource
    private DictionaryListMapper dictionaryListMapper;

    /*多条件分页查询字典管理*/
    public Map<String, Object> querySaleChanceByParams(DictionaryListQuery dictionaryListQuery) {
        Map<String, Object> map = new HashMap<>();
        //开启分页
        PageHelper.startPage(dictionaryListQuery.getPage(), dictionaryListQuery.getLimit());
        //得到对应的分页对象
        PageInfo<DictionaryList> pageInfo = new PageInfo<>(dictionaryListMapper.selectByParams(dictionaryListQuery));
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }
    /*字典管理数据添加*/
    @Transactional(propagation = Propagation.REQUIRED)
    public void addDictionaryList(DictionaryList dictionaryList){
        //1.参数校验
        checkParams(dictionaryList.getDictionaryName(),dictionaryList.getDictionaryValue(),null);
        //2.设置相关参数默认值
        dictionaryList.setIsValid(1);
        dictionaryList.setUpdateDate(LocalDateTime.now());
        dictionaryList.setCreateDate(LocalDateTime.now());
        //执行添加操作，，判断受影响的行数结果
        AssertUtil.isTrue(insertSelective(dictionaryList)<1,"添加字典管理失败");
    }
    /*字典数据更新*/
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDictionary(DictionaryList dictionaryList){
        //参数校验
        //通过id查询记录
        DictionaryList temp = dictionaryListMapper.selectByPrimaryKey(dictionaryList.getId());
        //判断是否为空
        AssertUtil.isTrue(null ==temp,"待更新记录不存在");
        checkParams(dictionaryList.getDictionaryName(),dictionaryList.getDictionaryValue(),null);
        //设置相关的参数的默认值
        dictionaryList.setUpdateDate(LocalDateTime.now());
        //执行添加操作，判断受影响的行数
        AssertUtil.isTrue(dictionaryListMapper.updateByPrimaryKeySelective(dictionaryList) !=1,"更新字典数据失败");
    }
    //基本参数校验
    private void checkParams(String dictionaryName, String dictionaryValue,String createDate) {
        AssertUtil.isTrue(StringUtils.isBlank(dictionaryName),"请输入字典名");
        AssertUtil.isTrue(StringUtils.isBlank(dictionaryValue),"请输入字典值");

    }
    /*删除字典管理*/
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteDictionary(Integer [] ids){
        //判断id是否为空
        AssertUtil.isTrue(ids==null || ids.length<1,"待删除记录不存在");
        //执行删除更新操作判断受影响的行数
        AssertUtil.isTrue(dictionaryListMapper.deleteBatch(ids)!=ids.length,"字典数据删除失败");
    }
}